import 'package:cat_eye/config/color_config.dart';
import 'package:cat_eye/config/string_config.dart';
import 'package:cat_eye/provider/app_Info_provider.dart';
import 'package:cat_eye/util/sp_util.dart';
import 'package:cat_eye/util/util.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

/*
* @Package: root_page.my_page
* @ClassName: my_setting_widget
* @Author: szj
* @CreateDate: 8/4/21 10:07 AM
* TODO 我的-设置页面
*/
class MySettingWidget extends StatefulWidget {
  const MySettingWidget({Key key}) : super(key: key);

  @override
  _MySettingWidgetState createState() => _MySettingWidgetState();
}

class _MySettingWidgetState extends State<MySettingWidget> {
  //夜间模式
  bool isNight = true;

  @override
  void initState() {
    super.initState();
    //判断当前是否是夜间模式
    Provider.of<AppInfoProvider>(context, listen: false)?.themeMode == "dark"
        ? isNight = true
        : isNight = false;
  }

  String _colorKey;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: appBar(
        StringConfig.SETTING,backgroundColor: ColorConfig.themeColor
      ),
      body: Container(
        child: Column(
          children: [
            initSwitchListTile(context),
            //颜色主题
            initExpansionTile(),
          ],
        ),
      ),
    );
  }

  //夜间模式
  SwitchListTile initSwitchListTile(BuildContext context) {
    return SwitchListTile(
      title: Text(
          "${StringConfig.NIGHT_MODE}${isNight ? StringConfig.TURNED_ON : StringConfig.NOT_OPEN}"),
      //按钮打开颜色
      activeColor: ColorConfig.themeColor,
      onChanged: (bool value) {
        setState(() {
          isNight = value;
          //改变主题模式
          Provider.of<AppInfoProvider>(context, listen: false)
              .setNight(value ? "dark" : "light");

          //SP保存主题模式
          SpUtil.setData<bool>(StringConfig.SP_IS_NIGHT_MODE, value);
        });
      },
      value: isNight,
    );
  }

  //颜色主题
  Widget initExpansionTile() {
    return ExpansionTile(
      //主题
      leading: Icon(
        Icons.colorize,
        color: ColorConfig.themeColor,
      ),
      title: Text(
        StringConfig.COLOR_THEME,
        style: TextStyle(color: ColorConfig.themeColor),
      ),
      initiallyExpanded: true,
      trailing: Icon(
        Icons.expand_more,
        color: ColorConfig.themeColor,
      ),
      //展开关闭监听
      onExpansionChanged: (v) {},
      children: <Widget>[
        Padding(
          padding: EdgeInsets.only(left: 10, right: 10, bottom: 10),
          //设置颜色布局
          child: initWrap(),
        )
      ],
    );
  }

  Wrap initWrap() {
    return Wrap(
      spacing: 8,
      runSpacing: 8,
      children: ColorConfig.themeColorMap.keys.map((key) {
        Color value = ColorConfig.themeColorMap[key];
        return InkWell(
          onTap: () {
            Provider.of<AppInfoProvider>(context, listen: false).setTheme(key);
            setState(() {
              //保存默认颜色
              ColorConfig.themeColor = ColorConfig.themeColorMap[key];

              _colorKey = key;

              //保存默认颜色
              SpUtil.setData<String>(StringConfig.SP_IS_COLOR_THEME, _colorKey);
            });
          },
          child: Container(
            width: 40,
            height: 40,
            color: value,
            child: _colorKey == key
                ? Icon(
                    Icons.done,
                    color: Colors.white,
                  )
                : null,
          ),
        );
      }).toList(),
    );
  }
}
